package cn.edu.bupt.freewings.forum.server.rest;

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.Consumes;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.apache.log4j.Logger;
import cn.edu.bupt.freewings.forum.common.PostType;
import cn.edu.bupt.freewings.forum.common.TopicType;
import cn.edu.bupt.freewings.forum.common.CommonType;
import cn.edu.bupt.freewings.forum.persistence.EntityManagerHelper;
import cn.edu.bupt.freewings.forum.persistence.Post;
import cn.edu.bupt.freewings.forum.persistence.PostDAO;
import cn.edu.bupt.freewings.forum.server.Utility.Converter;
import cn.edu.bupt.freewings.forum.server.Utility.MessageConstants;
import org.apache.log4j.Logger;

/**
 * @author hubaiyu (hubaiyu@gmail.com)
 *
 */
@Path("/post")
public class PostResource {

	private Logger logger = Logger.getLogger(PostResource.class);
	@PUT
	@Path("/new")
	@Consumes({"application/proto", "application/xml"})
	@Produces({"application/proto", "application/xml"})
	public CommonType.Empty putPost(PostType.Post post){
		logger.info("Try save post:" + post.getText());
		if(post.hasId()){
			logger.warn("New Post Shouldn't have a id, but get " + post.getId());
		}
		logger.info("TX begin");
		EntityManagerHelper.beginTransaction();
		new PostDAO().save(Converter.postTypeToPost(post));
		EntityManagerHelper.commit();
		logger.info("TX end");
		return MessageConstants.EMPTY_MESSAGE;
	}
}
